﻿using LoadClient.YUAN.C;
using LoadClient.YUAN.E;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;

namespace LoadClient.YUAN.F
{
    public class ApiManager
    {
        private static string host = $"{COP.GetAppSettingsValue("ApiUrl")}/api/LoadClient";


        public static List<LoadInfo> GetRealTime(int stNo)
        {
            var res = HttpHelper.Get($"{host}/GetRealTime/{stNo}");
            if (res != null)
            {
                var result = JsonConvert.DeserializeObject<ApiResult<List<LoadInfo>>>(res);
                return result.Data;
            }
            return null;
        }

        public static List<LoadReport> GetLoadReportListByDate(DateTime startTime, DateTime endTime)
        {
            var res = HttpHelper.Get($"{host}/GetLoadReportListByDate?startTime={startTime}&endTime={endTime}");
            if (res != null)
            {
                var result = JsonConvert.DeserializeObject<ApiResult<List<LoadReport>>>(res);
                return result.Data;
            }
            return null;
        }
        public static List<LoadReport> GetLoadReportList()
        {
            var res = HttpHelper.Get($"{host}/GetLoadReportList");
            if (res != null)
            {
                var result = JsonConvert.DeserializeObject<ApiResult<List<LoadReport>>>(res);
                return result.Data;
            }
            return null;
        }
        public static List<LoadReport> GetLoadReportListByFbarID(int fbarId)
        {
            var res = HttpHelper.Get($"{host}/GetLoadReportListByFbarID/{fbarId}");
            if (res != null)
            {
                var result = JsonConvert.DeserializeObject<ApiResult<List<LoadReport>>>(res);
                return result.Data;
            }
            return null;
        }
        public static List<LoadReport> GetLoadReportListBySerial(int serial)
        {
            var res = HttpHelper.Get($"{host}/GetLoadReportListBySerial/{serial}");
            if (res != null)
            {
                var result = JsonConvert.DeserializeObject<ApiResult<List<LoadReport>>>(res);
                return result.Data;
            }
            return null;
        }
        public static bool DoCommand(int objv, string address, int index)
        {
            var res = HttpHelper.Post($"{host}/Command", new Cmd { Val = objv, Address = address, Index = index }.ToJson());
            if (res != null)
            {
                var result = JsonConvert.DeserializeObject<ApiResult<bool>>(res);
                return result.Data;
            }
            return false;
        }
        public static List<Product> GetProductList()
        {
            var res = HttpHelper.Get($"{host}/GetProductList");
            if (res != null)
            {
                var result = JsonConvert.DeserializeObject<ApiResult<List<Product>>>(res);
                return result.Data;
            }
            return null;
        }
        public static List<Product> GetProductListVisable()
        {
            var res = HttpHelper.Get($"{host}/GetProductListVisable");
            if (res != null)
            {
                var result = JsonConvert.DeserializeObject<ApiResult<List<Product>>>(res);
                return result.Data;
            }
            return null;
        }
        public static List<LoadInfo> GetLoadInfoList(int stNo)
        {
            var res = HttpHelper.Get($"{host}/GetLoadInfoList/{stNo}");
            if (res != null)
            {
                var result = JsonConvert.DeserializeObject<ApiResult<List<LoadInfo>>>(res);
                return result.Data;
            }
            return null;
        }
        public static Product GetProductByPID(int pid)
        {
            var res = HttpHelper.Get($"{host}/GetProductByPID/{pid}");
            if (res != null)
            {
                var result = JsonConvert.DeserializeObject<ApiResult<Product>>(res);
                return result.Data;
            }
            return null;
        }
        public static int GetLoadReportMaxSerial()
        {
            var res = HttpHelper.Get($"{host}/GetLoadReportMaxSerial");
            if (res != null)
            {
                var result = JsonConvert.DeserializeObject<ApiResult<int>>(res);
                return result.Data;
            }
            return 0;
        }

        public static int InsertLoadReport(LoadReport loadReport)
        {
            var res = HttpHelper.Post($"{host}/InsertLoadReport", loadReport.ToJson());
            if (res != null)
            {
                var result = JsonConvert.DeserializeObject<ApiResult<int>>(res);
                return result.Data;
            }
            return 0;
        }

        public static bool InsertCommandRecord(CommandRecord cmdRecord)
        {
            var res = HttpHelper.Post($"{host}/InsertCommandRecord", cmdRecord.ToJson());
            if (res != null)
            {
                var result = JsonConvert.DeserializeObject<ApiResult<bool>>(res);
                return result.Data;
            }
            return false;
        }
    }
}
